<?php
namespace App\Utils\Other\Payment;

use Hyperf\Context\Context;
use Hyperf\Collection\Arr;

/**
 * 支付基类
 * @package App\Utils\Other\Payment
 */
abstract Class Base {

    /**
     * 设置配置
     * @param array|null $config
     * @return $this
     */
    public function setConfig(?array $config):static
    {
        if (!$config) {
            Context::set('pay_config', []);
            return $this;
        }

        $configData = $this->getConfig();
        $config = array_merge($configData, $config);
        Context::set('pay_config', $config);
        return $this;
    }

    /**
     * 获取配置
     * @param string|null $key
     * @param mixed $default
     * @return mixed
     */
    public function getConfig(?string $key=null, mixed $default=[]):mixed
    {
        $config = Context::get('pay_config', []);
        return $key?Arr::get($config, $key, $default):$config;
    }

    /**
     * 设置路径
     * @param string $path
     * @return $this
     */
    protected function setPath(string $path): static
    {
        $this->setConfig(['path' => $path]);
        return $this;
    }

    /**
     * 请求
     * @param array $params
     * @return array
     */
    abstract protected function request(array $params = []):array;

    /**
     * 响应处理
     * @param $response
     * @param array $params
     * @return array
     */
    abstract protected function response($response, array $params = []):array;

    /**
     * 获取参数
     * @param array $params
     * @return array
     */
    protected function params(array $params = []):array
    {
        return $params;
    }
}